<html>
<head>
    <script src="../../../../resources/ahem.js"></script>
    <style>
        #test {
            width: 400px;
            font-family: "Ahem";
        }
    </style>
    <script>
        if (window.testRunner)
            testRunner.dumpAsText();

        var _log = "";
        function log(msg)
        {
            _log += msg + "\n";
        }

        function swapInLog()
        {
            var element = document.getElementById('test');
            var parent = element.parentNode;
            parent.removeChild(element);
            var console = document.createElement("pre");
            console.textContent = _log;
            parent.appendChild(console);
        }

        var element;

        function test(x, y, __expectedContainer, __expectedOffset)
        {
            var expectedContainer = eval(__expectedContainer);
            var expectedOffset = eval(__expectedOffset);

            var range = document.caretRangeFromPoint(x, y);

            if (range.startContainer == expectedContainer) {
                log("PASS: range.startContainer == " + __expectedContainer + ".");
            } else {
                log("FAIL: range.startContainer == " + range.startContainer + ".");
            }

            if (range.startOffset == expectedOffset) {
                log("PASS: range.startOffset == " + __expectedOffset + ".");
            } else {
                log("FAIL: range.startOffset == " + range.startOffset + ".");
            }
        }

        function shouldBeNull(x, y)
        {
            var range = document.caretRangeFromPoint(x, y);

            if (range === null) {
                log("PASS: range is null.");
            } else {
                log("FAIL: range is not null.");
            }
        }

        window.onload = function()
        {
            element = document.getElementById('test');
            text = element.firstChild;

            var rect = element.getBoundingClientRect();

            log("Testing upper left");
            test(rect.left, rect.top, "element.firstChild", "0");

            log("Testing upper right");
            test(rect.left + rect.width, rect.top, "element.firstChild", "23");

            log("Testing lower left");
            test(rect.left, rect.top + rect.height - 1, "element.firstChild", "72");
            
            log("Testing lower right");
            test(rect.left + rect.width, rect.top + rect.height - 1, "element.firstChild", "95");

            log("Testing somewhere in the middle");
            test((rect.left + rect.width) / 2, (rect.top + rect.height) / 2, "element.firstChild", "36");

            log("Testing negative values");
            shouldBeNull(-10, 10);
            shouldBeNull(10, -10);
            shouldBeNull(-10, -10);

            log("Testing values larger than the viewport");
            shouldBeNull(window.innerWidth + 100, 10);
            shouldBeNull(10, window.innerHeight + 100);
            shouldBeNull(window.innerWidth + 100, window.innerHeight + 100);

            swapInLog();
        }
    </script>
</head>
<body>
<div id="test">xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx</div>
</body>
</html>
